// const db = uniCloud.database()
// const dbCmd = db.command
// const $ = db.command.aggregate
exports.main = async (event, context) => {
	if (event.leixing == 1) { 
		const now = new Date();
		const year = now.getFullYear();
		const month = (now.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的
		const currentDate = `${year}-${month}-01 00:00:00`;
		
	const dbJQL = uniCloud.databaseForJQL({
		event,
		context
	})
	const dbCmd = dbJQL.command;
	dbJQL.setUser({ // 指定后续执行操作的用户信息，此虚拟用户将同时拥有传入的uid、role、permission
			// uid: 'user-id', // 用户id
			role: ['admin'], // 指定当前执行用户的角色为admin。如果只希望指定为admin身份，可以删除uid和permission字段
			// permission: [] // 用户权限
		})
		const res1 = await dbJQL.collection('baokelist').where({
			zhuangtai:1
		}).count()
		const res2 = await dbJQL.collection('uni-id-users').where({
			role:"renkelaoshi",
		}).count()
		const res3 = await dbJQL.collection('baokelist').where({
			'zhuangtai':dbCmd.eq(1).or(dbCmd.eq(2))
		})
		// .field('zhuangtai,multiply(kechengshu,yikefeiyong) as xuefei')	
		.field('zhuangtai,multiply(subtract(kechengshu , yihexiaoshu),yikefeiyong) as xuefei,subtract(kechengshu , yihexiaoshu) as keshi')	
		.groupBy('zhuangtai')
		.groupField('sum(xuefei) as shengyuxuefei,sum(keshi) as shengyukeshi')
		.get()
		const res4 = await dbJQL.collection('zhangwuliushui').where({
			'riqi':dbCmd.gt(currentDate)
		})
		.field('biandong,jine')	
		// .field('zhuangtai,multiply(subtract(parseFloat(kechengshu) , parseFloat(yihexiaoshu)),parseFloat(yikefeiyong)) as xuefei')	
		.groupBy('biandong')
		.groupField('sum(jine) as jine')
		.get()
		
		
		return {res1,res2,res3,res4}
	}
};
